package com.atguigu.gmall.realtime.app.dwd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.util.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternFlatSelectFunction;
import org.apache.flink.cep.PatternFlatTimeoutFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.util.List;
import java.util.Map;

public class DwdTrafficUserJumpOutApp {


    //1  取得kafka 流 dwd_traffic_page_log
    //2  转成jsonObj
    //3  定义时间语义  事件时间
    //4    keyby
    //5   定义cep表达式
    //6   把cep表达式合并到流中
    //7   从流中提取表达命中结果 --> 1 匹配结果 2 超时结果
    //8   把结果侧输出为流
    //9   再把俩中结果合并
    //10  写入kafka
    public static void main(String[] args) throws Exception {
        //1  取得kafka 流 dwd_traffic_page_log
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        String sourceTopic="dwd_traffic_page_log";
        String groupId="dwd_traffic_user_jump_out_app";
        Long jumpTimeout=10000L;
        FlinkKafkaConsumer<String> kafkaConsumer = MyKafkaUtil.getKafkaConsumer(sourceTopic, groupId);

                // 测试数据
        DataStream<String> kafkaStrDS = env
            .fromElements(
                "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000} ",
                       "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"home\"},\"ts\":12000}",
                       "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":\"home\"},\"ts\":15000} ",
                       "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":\"detail\"},\"ts\":30000} ",
                       "{\"common\":{\"mid\":\"103\"},\"page\":{\"page_id\":\"home\"},\"ts\":42000}",
                       "{\"common\":{\"mid\":\"103\"},\"page\":{\"page_id\":\"home\"},\"ts\":48000}",
                       "{\"common\":{\"mid\":\"103\"},\"page\":{\"page_id\":\"home\"},\"ts\":50000}"
            );


       DataStreamSource<String> kafkaDstream= (DataStreamSource<String>) env.addSource(kafkaConsumer);
        //2  转成jsonObj
        SingleOutputStreamOperator<JSONObject> jsonObjDstream = kafkaDstream.map(JSON::parseObject);

        //3  定义时间语义  事件时间
        SingleOutputStreamOperator<JSONObject> jsonObjWithWatermarkDstream = jsonObjDstream.assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forMonotonousTimestamps()
                .withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
                    @Override
                    public long extractTimestamp(JSONObject jsonObject, long recordTimestamp) {
                        return jsonObject.getLong("ts");
                    }
                }));
        //4    keyby  mid
        KeyedStream<JSONObject, String> midKeyedDstream = jsonObjWithWatermarkDstream.keyBy(jsonObj -> jsonObj.getJSONObject("common").getString("mid"));
        //5   定义cep表达式
//        步骤1 ：  首次访问（last_page_id==null)
//        步骤2： 2.1  超时
//        2.2  紧邻访问（last_page_id==null)

        Pattern<JSONObject, JSONObject> jumpOutPattern = Pattern.<JSONObject>begin("first").where(
                new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) {  //是否是会话首次访问
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return  lastPageId==null||lastPageId.length()==0;
                    }
                }
        ).next("second").where(
                new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) {   //是否是会话首次访问
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return  lastPageId==null||lastPageId.length()==0;
                    }
                }
        ).within(Time.milliseconds(jumpTimeout)) ;

        //6   把cep表达式应用到流中
        PatternStream<JSONObject> patternStream = CEP.pattern(midKeyedDstream, jumpOutPattern);


        OutputTag<JSONObject> timeoutTag=new OutputTag<JSONObject>("timeoutTag"){};
        //7   从流中提取表达命中结果 --> 1 匹配结果 2 超时结果
        SingleOutputStreamOperator<JSONObject> twiceSessionStream = patternStream.flatSelect(
                timeoutTag, new PatternFlatTimeoutFunction<JSONObject, JSONObject>() {
                    @Override
                    public void timeout(Map<String, List<JSONObject>> pattern, long timeoutTimestamp, Collector<JSONObject> out) throws Exception {
                        List<JSONObject> firstList = pattern.get("first");  //超时数据提取
                        JSONObject jsonObject = firstList.get(0);
                        jsonObject.put("jump_ts",jsonObject.getLong("ts")+jumpTimeout); //增加一个跳出时间戳
                        out.collect(jsonObject);
                    }
                }, new PatternFlatSelectFunction<JSONObject, JSONObject>() {
                    @Override    // 两次会话数据提取
                    public void flatSelect(Map<String, List<JSONObject>> pattern, Collector<JSONObject> out) throws Exception {
                        List<JSONObject> firstList = pattern.get("first");
                           JSONObject firstJsonObj = firstList.get(0);

                           //提取第二次会话的事件的时间戳 作为第一个事件的跳出时间
                        List<JSONObject> secondList = pattern.get("second");
                        JSONObject secondJsonObj = secondList.get(0);

                        firstJsonObj.put("jump_ts",secondJsonObj.getLong("ts"));
                           out.collect(firstJsonObj);
                    }
                }

        );

        //8   把超时结果侧输出为流
        DataStream<JSONObject> timeoutStream = twiceSessionStream.getSideOutput(timeoutTag);
       // timeoutStream.print("timeout:::::");
       // twiceSessionStream.print("twice:::::");

        //9   再把俩中结果合并
         DataStream<JSONObject> jumpOutStream = twiceSessionStream.union(timeoutStream);


        //10  写入kafka
        jumpOutStream.map(json->JSON.toJSONString(json)).addSink(MyKafkaUtil.getKafkaProducer("dwd_traffic_user_jump_out"));
        env.execute();

    }
}
